/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: qian
 * Date: 2022-07-18
 * Time: 11:35
 */
public class BinarySearchTree {
    static class TreeNode{
        public int key;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int key){
            this.key=key;
        }

    }
    public TreeNode root;
    //二叉搜索树的插入
    public boolean insert(int key){
        if(root==null){
            root=new TreeNode(key);
            return true;
        }
        TreeNode cur=root;
        TreeNode parent=null;
        while (cur!=null){
            if(cur.key>key){
                parent=cur;
                cur=cur.left;
            }else if(cur.key<key){
                parent=cur;
                cur=cur.right;
            }else{
                return false;
            }
        }
        //此时cur=null;
        if(key> parent.key){
            parent.right=new TreeNode(key);
        }else {
            parent.left=new TreeNode(key);
        }
        return true;
    }
    public TreeNode search(int key){
        TreeNode cur=root;
        while (cur!=null){
            if(cur.key==key){
                return cur;
            }else if(cur.key>key){
                cur=cur.left;
            }else{
                cur=cur.right;
            }
        }
        return null;
    }
    //二叉搜索树的删除
    public boolean remove(int key){
        TreeNode cur=root;
        TreeNode parent=null;
        while (cur!=null){
            if(cur.key>key){
                parent=cur;
                cur=cur.left;
            }else if(cur.key==key){
                removeNode(parent,cur);
                return true;
            }else{
                parent=cur;
                cur=cur.right;
            }
        }
        return false;
    }
    public void removeNode(TreeNode parent,TreeNode cur){
        if(cur.left==null){
            if(cur==root){
                root=cur.right;
            }else if(parent.left==cur){
                parent.left=cur.right;
            }else{
                parent.right=cur.right;
            }
        }else if(cur.right==null){
            if(cur==root){
                root=cur.left;
            }else if(parent.left==cur){
                parent.left=cur.left;
            }else{
                parent.right=cur.left;
            }
        }else{
            //使用替换法
            //找到右边的最小值或者左边的最大值替换掉要删除的值，然后删除这个节点
            TreeNode targetParent=cur;
            TreeNode target=cur.right;
            while (target.left!=null){
                targetParent=target;
                target=target.left;
            }
            cur.key=target.key;
            if(targetParent.left==target){
                targetParent.left=target.right;
            }else{
                targetParent.right=target.right;
            }
        }
    }
    //中序遍历
    public void inOrder(TreeNode root){
        if(root==null){
            return;
        }
        inOrder(root.left);
        System.out.print(root.key+" ");
        inOrder(root.right);
    }
}
